/**
 * Project Name:kany-utils
 * File Name:HttpClientSubmitHandler.java
 * Package Name:me.kany.utils.httpclient.handler
 * Date:2016年5月11日上午9:44:02
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.utils.httpclient.handler;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

import me.kany.utils.httpclient.HttpClientManager;

/**
 * ClassName:HttpClientSubmitHandler <br/>
 * Function: HttpClient提交类. <br/>
 * Date:     2016年5月11日 上午9:44:02 <br/>
 * @author   Jason.Wang
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
public class HttpClientSubmitHandler {
    private static RequestConfig requestConfig;
    private static final int MAX_TIMEOUT = 10000;

    static {
        RequestConfig.Builder configBuilder = RequestConfig.custom();
        // 设置连接超时
        configBuilder.setConnectTimeout(MAX_TIMEOUT);
        // 设置读取超时
        configBuilder.setSocketTimeout(MAX_TIMEOUT);
        // 设置从连接池获取连接实例的超时
        configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
    }
    
    
	/**
	 * 
	 * buildGetRequest:根据当前用户提出的请求参数地址返回对应的结果. <br/>
	 * 
	 * @author Jason.Wang
	 * @param requestURL
	 * @param jsonObject
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public static String buildGetRequest(String requestURL, JSONObject jsonObject) throws Exception {
		// 待请求参数数组
		HttpClient httpclient = null;
		httpclient = HttpClientManager.getSSLHttpClient();
		HttpGet httpGet = new HttpGet(requestURL);
		String requestData = "";
		if (null != jsonObject) {
			requestData = jsonObject.toJSONString();
		}
		// Construct a string entity
		StringEntity paramsEntity = new StringEntity(requestData, Charset.forName("UTF-8"));
		// Set XML entity
		paramsEntity.setContentEncoding("UTF-8");
		paramsEntity.setContentType("application/json");
		httpGet.setConfig(requestConfig);
		// Set content type of request header
		httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
		httpGet.addHeader("Content-Type", "application/json");
		// Execute request and get the response
		HttpResponse response = httpclient.execute(httpGet);
		// CloseableHttpResponse response = httpclient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		int status = response.getStatusLine().getStatusCode();
		StringBuilder resultXml = new StringBuilder();
		if (entity != null) {
			InputStreamReader inputStreamReader = new InputStreamReader(entity.getContent(), "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String text;
			while ((text = bufferedReader.readLine()) != null) {
				resultXml.append(text);
			}
			inputStreamReader.close();
			bufferedReader.close();
		}
		EntityUtils.consume(entity);
		httpGet.abort();
		String result = resultXml.toString();
		if (200 == status) {
			return result;
		} else {
			throw new RuntimeException(status + ":" + result);
		}
	}
    
	/**
	 * 
	 * buildPostRequest:根据当前用户提出的请求参数地址返回对应的结果. <br/>
	 * 
	 * @author Jason.Wang
	 * @param requestURL
	 * @param jsonObject
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public static String buildPostRequest(String requestURL, JSONObject jsonObject) throws Exception {
		// 待请求参数数组
		HttpClient httpclient = null;
		httpclient = HttpClientManager.getSSLHttpClient();
		HttpPost httpPost = new HttpPost(requestURL);
		String requestData = "";
		if (null != jsonObject) {
			requestData = jsonObject.toJSONString();
		}
		// Construct a string entity
		StringEntity paramsEntity = new StringEntity(requestData, Charset.forName("UTF-8"));
		// Set XML entity
		paramsEntity.setContentEncoding("UTF-8");
		paramsEntity.setContentType("application/json");
		httpPost.setEntity(paramsEntity);
		httpPost.setConfig(requestConfig);
		// Set content type of request header
		httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
		httpPost.addHeader("Content-Type", "application/json");
		// Execute request and get the response
		HttpResponse response = httpclient.execute(httpPost);
		// CloseableHttpResponse response = httpclient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		int status = response.getStatusLine().getStatusCode();
		StringBuilder resultXml = new StringBuilder();
		if (entity != null) {
			InputStreamReader inputStreamReader = new InputStreamReader(entity.getContent(), "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String text;
			while ((text = bufferedReader.readLine()) != null) {
				resultXml.append(text);
			}
			inputStreamReader.close();
			bufferedReader.close();
		}
		EntityUtils.consume(entity);
		httpPost.abort();
		String result = resultXml.toString();
		if (200 == status) {
			return result;
		} else {
			throw new RuntimeException(status + ":" + result);
		}
	}
}
